/*!
 * \file      radio.h
 *
 * \brief     Radio driver API definition
 *
 * \copyright Revised BSD License, see section \ref LICENSE.
 *
 * \code
 *                ______                              _
 *               / _____)             _              | |
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 *               _____) ) ____| | | || |_| ____( (___| | | |
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 *              (C)2013-2017 Semtech
 *
 * \endcode
 *
 * \author    Miguel Luis ( Semtech )
 *
 * \author    Gregory Cristian ( Semtech )
 */
#ifndef __RADIO_H__
#define __RADIO_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
#include <stdbool.h>
#include <sx126x.h>
#include "LoRa_model.h"
/*!
 * Radio driver supported modems
 */
typedef enum
{
    MODEM_FSK = 0,
    MODEM_LORA,
}RadioModems_t;

/*!
 * Radio driver internal state machine states definition
 */
typedef enum
{
    RF_IDLE = 0,   //!< The radio is idle
    RF_RX_RUNNING, //!< The radio is in reception state
    RF_TX_RUNNING, //!< The radio is in transmission state
    RF_CAD,        //!< The radio is doing channel activity detection
}RadioState_t;

/*!
 * \brief Gets the current Radio OperationMode variable
 *
 * \retval      RadioOperatingModes_t last operating mode
 */
RadioOperatingModes_t SX126xGetOperatingMode( lora_t *lora );

/*!
 * \brief Sets/Updates the current Radio OperationMode variable.
 *
 * \remark WARNING: This function is only required to reflect the current radio
 *                  operating mode when processing interrupts.
 *
 * \param [in] mode           New operating mode
 */
void SX126xSetOperatingMode( lora_t *lora ,RadioOperatingModes_t mode );

/*!
 * \brief Initializes the TCXO power pin.
 */
void SX126xIoTcxoInit(lora_t *lora);

/*!
 * \brief Gets the Defines the time required for the TCXO to wakeup [ms].
 *
 * \retval time Board TCXO wakeup time in ms.
 */
uint32_t SX126xGetBoardTcxoWakeupTime( lora_t *lora  );

/*!
 * \brief Initializes RF switch control pins.
 */
void SX126xIoRfSwitchInit( lora_t *lora  );

/*!
 * \brief Initializes the RF Switch I/Os pins interface
 */
void SX126xAntSwOn( lora_t *lora  );

/*!
 * \brief De-initializes the RF Switch I/Os pins interface
 *
 * \remark Needed to decrease the power consumption in MCU low power modes
 */
void SX126xAntSwOff( lora_t *lora  );

/*!
 * \brief Gets the device ID
 *
 * \retval id Connected device ID
 */
uint8_t SX126xGetDeviceId( lora_t *lora  );

/*!
 * \brief Sets the radio output power.
 *
 * \param [IN] power Sets the RF output power
 */
void SX126xSetRfTxPower( lora_t *lora ,int8_t power );

/*!
 * \brief Write a single byte of data to the radio memory
 *
 * \param [in]  address       The address of the first byte to write in the radio
 * \param [in]  value         The data to be written in radio's memory
 */
void SX126xWriteRegister(lora_t *lora , uint16_t address, uint8_t value );

/*!
 * \brief Read a single byte of data from the radio memory
 *
 * \param [in]  address       The address of the first byte to write in the radio
 *
 * \retval      value         The value of the byte at the given address in radio's memory
 */
uint8_t SX126xReadRegister( lora_t *lora ,uint16_t address );

/*!
 * \brief Wakes up the radio
 */
void SX126xWakeup( lora_t *lora  );

/*!
 * \brief Send a command that write data to the radio
 *
 * \param [in]  opcode        Opcode of the command
 * \param [in]  buffer        Buffer to be send to the radio
 * \param [in]  size          Size of the buffer to send
 */
void SX126xWriteCommand( lora_t *lora ,RadioCommands_t opcode, uint8_t *buffer, uint16_t size );

/*!
 * \brief Send a command that read data from the radio
 *
 * \param [in]  opcode        Opcode of the command
 * \param [out] buffer        Buffer holding data from the radio
 * \param [in]  size          Size of the buffer
 *
 * \retval status Return command radio status
 */
uint8_t SX126xReadCommand(lora_t *lora , RadioCommands_t opcode, uint8_t *buffer, uint16_t size );

/*!
 * \brief Tx timeout timer callback
 */
void RadioOnTxTimeoutIrq( lora_t *lora ,void* context );

/*!
 * \brief Rx timeout timer callback
 */
void RadioOnRxTimeoutIrq( lora_t *lora ,void* context );

/*!
 * \brief Radio driver
 *
 * \remark This variable is defined and initialized in the specific radio
 *         board implementation
 */

void Lora_Setup_SX126x_noname_f(lora_t *lora,lora_msg_cb byte_cb,lora_msg_cb gpio_and_delay);

void sx126xLoraInit(lora_t *lora, lora_config_t *stting);
uint8_t sx126xRxData(lora_t *lora, uint8_t *buf, uint8_t *len);
uint8_t sx126xSend(lora_t *lora,uint32_t fr,uint8_t *buffer,uint8_t size,uint32_t time_out); //
uint8_t sx126xWaitSend(lora_t *lora,uint8_t ms);
uint8_t sx126xStartSend(lora_t *lora, uint32_t fr, uint8_t *buffer, uint8_t size, uint32_t time_out);
void sx126xStartRx(lora_t *lora,uint32_t fr);
#ifdef __cplusplus
}
#endif

#endif // __RADIO_H__
